//
//  ApplyViewController.swift
//  shaohuo
//
//  Created by hujinbiao on 2018/1/16.
//  Copyright © 2018年 lchenc3. All rights reserved.
//

import Foundation

class ApplyViewController : BaseViewController {
    
    var applyButton: UIButton?
    
    var webViewController : RxWebViewController?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.white
        
        applyButton = UIButton(type: UIButtonType.custom)
        applyButton?.frame = CGRect(x: 10, y: view.frame.size.height * 0.5, width: view.frame.size.width - 20, height: 40)
        applyButton?.setTitle("点击申请成为捎货人", for: UIControlState())
        applyButton?.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        applyButton?.setTitleColor(UIColor.darkGray, for: UIControlState())
        view.addSubview(applyButton!)
        applyButton?.addTarget(self, action: #selector(ApplyViewController.SELApplyButton), for: UIControlEvents.touchUpInside)
    }
    
    
    @objc func SELApplyButton() -> Void {
        
        if !UserTicketModel.sharedInstance.isLogin {
            
            let loginVc = LoginController()
            self.present(loginVc, animated: true, completion: nil)
            return
        }
        
        if UserTicketModel.sharedInstance.meIsShipper == false {
            showExamViewController()
        }else if UserTicketModel.sharedInstance.normalUser == false {
            let alertView = UIAlertView(title: "捎货提醒", message: "您已是商家/加盟商/渠道号用户，不能申请成为捎货人，请另外申请注册新账号", delegate: nil, cancelButtonTitle: "确定")
            alertView.show()
            
        }else {
            showExamViewController()
        }
    }
    
    func showExamViewController() {
        
        let uid : String? = UserTicketModel.sharedInstance.uid ?? ""
        let business_url = "\(ShipperExamURLString)?uid=\(uid ?? "")"
        webViewController = RxWebViewController(url: URL(string: business_url))
        webViewController?.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(webViewController!, animated: true)
        webViewController!.setJavascriptDelegate(self)
        
    }
}

extension ApplyViewController : SHWebJavaScriptBridgeDelegate {
    
    func shWebJavaScriptBridgePageAction(_ dicData: [AnyHashable: Any]!) {
        
        print("SHWebJavaScriptBridgePageAction: \(dicData)")
        
        let target =  dicData["target"] as? String
        ///提交资料
        if target == "UploadInfo" {
            self.pushUploadInfoViewController(viewType: .add, title: "提交资料")
        }
            ///查看提交资料
        else if target == "ViewUploadInfo" {
            self.pushUploadInfoViewController(viewType: .show, title: "查看提交资料")
        }
        else if target == "UpdateUploadInfo" {
            self.pushUploadInfoViewController(viewType: .update, title: "修改提交资料")
        }
    }
    
    /**
     提交资料
     */
    func pushUploadInfoViewController(viewType: ApplyInfoViewType,title: String) {

        let board = UIStoryboard(name: "ApplyInfoViewController", bundle: nil)
        let applyinfoController = board.instantiateInitialViewController() as! ApplyInfoViewController
        applyinfoController.title = title
        applyinfoController.viewActionType = viewType
        navigationController?.pushViewController(applyinfoController, animated: true)

        if viewType == .update {
            if self.webViewController?.webView.canGoBack == true {
                self.webViewController?.webView.goBack()
            }
        }
    }
}
